<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model{
    // 退款常量
    const REFUND_STATUS_PENDING = 'pending';
    const REFUND_STATUS_APPLIED = 'applied';
    const REFUND_STATUS_PROCESSING = 'processing';
    const REFUND_STATUS_SUCCESS = 'success';
    const REFUND_STATUS_FAILED = 'failed';

    // 邮寄常量
    const SHIP_STATUS_PENDING = 'pending';
    const SHIP_STATUS_DELIVERED = 'delivered';
    const SHIP_STATUS_RECEIVED = 'received';

    // 退款状态映射
    public static $refundSatusMap = [
        self::REFUND_STATUS_PENDING     => '未退款',
        self::REFUND_STATUS_APPLIED     => '已申请退款',
        self::REFUND_STATUS_PROCESSING  => '退款中',
        self::REFUND_STATUS_SUCCESS     => '退款成功',
        self::REFUND_STATUS_FAILED      => '退款失败',
    ];

    // 邮寄状态映射
    public static $shipStatusMap = [
        self::SHIP_STATUS_PENDING => '未发货',
        self::SHIP_STATUS_DELIVERED => '已发货',
        self::SHIP_STATUS_RECEIVED => '已收货',
    ];

    protected $fillable = [
        'no','address','total_amount','remark','paid_at','payment_method','payment_no','refund_status','refund_no','closed','reviewed','ship_status','ship_data','extra',
    ];

    protected $casts = [
        'closed' => 'boolean',
        'reviewed' => 'boolean',
        'address' => 'json',
        'ship_data' => 'json',
        'extra' => 'json',
    ];

    protected $dates = [
        'paid_at',
    ];

    // 模型监听，也可以使用observer 类来进行监听
    protected static function boot(){
        parent::boot();
        // 监听模型创建事件，在写入数据库之前触发
        static::creating(function($model){
            // 如果模型的 no 字段为空
            if(!$model->no){
                // 调用findAvailableNo 生成订单流水号
                $model->no = static::findAvailableNo();
                // 如果生成失败，则终止创建订单
                if($model->no){
                    return false;
                }
            }
        });
    }

    public function user(){
        return $this->belongsTo(User::class);
    }

    public function items(){
        return $this->hasMany(OrderItem::class);
    }

    public static function findAvailableNo(){
        // 订单流水号前缀
        $prefix = date('YmdHis');
        for($i = 0;$i < 10;$i++){
            //随机生成 6 位的数字
            $no = $prefix.str_pad(random_int(0,999999),6,'0',STR_PAD_LEFT);
            // 判断是否已经存在
            // TODO 可以将流水号放入redis 中，缓存5分钟。
            if(!static::query()->where('no',$no)->exists()){
                return $no;
            }
        }
        Log::warning(sprintf('find order no failed'));
        return false;
    }

}
